package com.zhxy.util.service.auth;

import java.util.Map;
import java.util.Set;

import com.zhxy.util.api.auth.Permission;


public class LocalPermissionSession implements Permission {
	
	public static final long UNLIMIT_DATE = Long.MAX_VALUE;
	
	private static final String[] EMPTY_SET = new String[0];
	
	private NamePwdAuthentication service;
	
	private String id;
	
	private String token;
	
	private String name;
	
	private Map<String, Object> attributes;
	
	private Set<String> permissions;
	
	public LocalPermissionSession() {
	}
	
	public LocalPermissionSession(NamePwdAuthentication service) {
		this.service = service;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	
	@Override
	public String getToken() {
		return token;
	}
	
	public void setToken(String token) {
		this.token = token;
	}

	@Override
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	//@Override
	public String[] getAttributeKeys() {
		if (attributes == null) {
			return EMPTY_SET;
		}
		return (String[])attributes.keySet().toArray();
	}
	
	//@Override
	public Object getAttribute(String name) {
		if (attributes == null) {
			return EMPTY_SET;
		}
		return attributes.get(name);
	}

	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}

	@Override
	public String[] getEntries() {
		
		if (service == null) 
			return EMPTY_SET;
		
		if (permissions == null) {
			permissions = service.queryPermissions(token);
		}
		return permissions.toArray(new String[permissions.size()]);
	}

}
